<?php

/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/15
 * Time: 9:24
 */

namespace App\Services\Api;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\CompanyModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\JobStaffModel;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\Admin\YzmModel;
use App\Models\JobsCompanysModel;
use App\Models\JobsModel;
use App\Models\LogModel;
use App\Models\OrderCommentModel;
use App\Models\UploadPicModel;
use App\Models\User\User_basechanpin_gsModel;
use App\Models\User\UsergongchangModel;
use App\Models\User\UserGongsiModel;
use App\Models\User\UserLevelModel;
use App\Models\User\UserModel;
use App\Models\User\Users_baseinfo_gsModel;
use App\Models\User\Users_gongsihuanjing_gsModel;
use App\Models\User\Users_zhengjianshangchuan_gsModel;
use App\Models\UserStaffModel;
use App\Services\OrdersPayService;
use App\Utilities\Imgcompress;
use Illuminate\Http\Request;
use App\Models\MoneyBilModel;
use DB;
use App\Models\User\UserOrdersModel;
use App\Services\Msg\MsgService;

/// App\Services\Api\User@
class User extends ApiBase
{

    public function getuserinfo(Request $request)
    {
        $uid = $request->input('uid');
        /* if (strpos($uid, 'mc_') !== false) {
             $uid = substr($uid, 3);
         }*/
        if ($uid == 'my') {
            $uid = OA::$user->getId();
        }

        $info = \Illuminate\Support\Facades\Cache::remember('getuserinfo' . $uid,
            0.2, function () use ($uid) {

                $user = UserModel::getinfo($uid);
                $info = [
                    'head' => $user['head'],
                    'name' => $user['name'],
                    'id' => $user['id'],
                    'sex' => $user['sex'],

                    'is_company' => $user['is_company'],
                    'is_factory' => $user['is_factory'],
                    'is_yingpin' => $user['is_yingpin']
                ];
                return $info;
            });
        OA::Json(0, $info);
    }

    /**
     *获取当前用户等级
     */
    public function getuserLevel()
    {
        OA::Json(0, UserLevelModel::getUserLevel(OA::$user->getId()));
    }


    /**
     * 修改头像
     */
    public function setUserHead(Request $request)
    {
        $upload = new UploadPicModel();
        $data = $upload->upPic($request->post('base64'));
        $uid = OA::$user->getId();
        $count = UserModel::where([
            'id' => $uid
        ])->update([
            'head' => $data['img']
        ]);
        if ($count == 1) {
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '系统错误');
        }

    }


    /**
     * 获取当前登录用户的信息
     */
    public function getNowUserInfo(Request $request)
    {
        // OA::Json(0,null);

        $info = OA::$user->getCacheModel();

        if ($info) {
            unset($info['money']);
            unset($info['jiguang_pass']);
            $info['head'] = Fun::pImg($info['head']);
            if ($request->post('shenhe') == 'true') {
                $info['is_company_shenhe'] = CompanyModel::getShenhe('is_company', $info['id']);
                $info['is_factory_shenhe'] = CompanyModel::getShenhe('is_factory', $info['id']);
                $info['is_yingpin_shenhe'] = CompanyModel::getShenhe('is_yingpin', $info['id']);
            }
            OA::Json(0, $info);
        } else {
            OA::Json(0, null);
        }

    }


    public function upSwitchFun(Request $request)
    {
        $name = $request->post('name');
        $value = $request->post('value');
        if ($name != 'is_company' && $name != 'is_factory' && $name != 'is_yingpin') {
            OA::Json(1, '参数错误');
        }
        if ($value == 'true') {
            $value = 1;
        } else {
            $value = 0;
        }
        $uid = OA::$user->getId();
        if ($value) {
            $shenhe = CompanyModel::getShenhe($name, $uid) == CompanyModel::shenhe_yes;
            if (!$shenhe) {
                OA::Json(0, 'no');
            }
        }
        UserModel::where([
            'id' => $uid
        ])->update([
            $name => $value
        ]);
        if ($name == 'is_yingpin') {
            UserStaffModel::where([
                'uid' => $uid
            ])->update([
                'visible' => $value
            ]);
        } elseif ($name == 'is_company') {///公司类型
            UserGongsiModel::where([
                'uid' => $uid,
                ///   'type'=>CompanyModel::type_gongsi
            ])->update([
                'visible' => $value
            ]);
        } elseif ($name == 'is_factory') {//工厂类型
            UsergongchangModel::where([
                'uid' => $uid,
                ///    'type'=>CompanyModel::type_gongchang
            ])->update([
                'visible' => $value
            ]);
        }
        OA::$user->clearCacheModel();
        OA::Json(0, 'ok');
    }

    /**
     * 修改密码
     */
    public function ChangePassword(Request $request)
    {
        /** yzm:yzm,
         * pass:pwd1
         */
        $yzm = $request->post('yzm');
        $pass = $request->post('pass');
        if (!$yzm) {
            OA::Json(1, '验证码错误');
        }
        if (!$pass) {
            OA::Json(1, '密码参数错误');
        }
        $user = OA::$user->getModel();
        if (!$user || empty($user['phone'])) {
            OA::Json(1, '系统错误,请和管理员联系');
        }
        $phone = $user['phone'];
        if (!YzmModel::isExists($phone, $yzm)) {
            OA::Json(1, '验证码输入错误,或者已经失效');
        }
        $userModel = UserModel::where('id', OA::$user->getId())->first();
        $userModel->password = \Hash::make($pass);
        $res = $userModel->save();
        if ($res) {
            OA::Json(0);
        } else {
            OA::Json(1, '系统错误,密码修改错误');
        }
    }


    /**
     * 设置用户的类型和介绍人的手机号码
     * introducer string 表示介绍人的手机号码
     * type int          表示用户类型101表示公司;102表示工厂;103表示个人应聘
     */
    public function setUserTypeIntroducer(Request $request)
    {

        $user = OA::$user->getModel();
        $user->introducer = $request->post('introducer');
        $user->type = $request->post('type');
        $r = $user->update();
        if ($r === false) {
            OA::Json(1, '系统错误,请重试');
        }
        OA::Json(0, 'ok');
    }


    /**
     *    获取公司资料完善情况
     */
    public function getusers_info_gs(Request $request)
    {
        $one = Users_baseinfo_gsModel::where([
            'id' => OA::$user->getId()
        ])->select([
            'isbase', 'iszhengjian', 'isgongsichanpin',
            'ischanpinfengge', 'iszhiliangbiaozhun', 'isgongsihuanjing',
        ])->first();
        if (!$one) {
            OA::Json(0, [
                'isbase' => 0,
                'iszhengjian' => 0,
                'isgongsichanpin' => 0,

                'ischanpinfengge' => 0,
                'iszhiliangbiaozhun' => 0,
                'isgongsihuanjing' => 0,
            ]);
        } else {
            OA::Json(0, $one->toArray());
        }
    }


    /**
     *获取公司基本信息
     */
    public function getusers_baseinfo_gs(Request $request, $post, $returnrray)
    {
        $usercom = UserGongsiModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if ($usercom) {
            $returnrray['comCity'] = $usercom->comCity;
            $returnrray['com_address'] = $usercom->com_address;
            $returnrray['com_name'] = $usercom->com_name;
            $returnrray['com_tel'] = $usercom->com_tel;
            $returnrray['legal_mobile'] = $usercom->legal_mobile;
            $returnrray['legal_name'] = $usercom->legal_name;
            $returnrray['legal_sex'] = $usercom->legal_sex;
            $returnrray['position'] = $usercom->position;
            $returnrray['detail'] = $usercom->detail;
            $nianxiaoshou1 = $usercom->nianxiaoshou;
            $returnrray['nianxiaoshou1'] = 0;
            $returnrray['nianxiaoshou2'] = 0;
            if ($nianxiaoshou1 && $nianxiaoshou1 != '') {
                $list = explode(',', $nianxiaoshou1);
                $returnrray['nianxiaoshou1'] = $list[0] ?? 0;
                $returnrray['nianxiaoshou2'] = $list[1] ?? 0;
            }
        }

        OA::Json(0, $returnrray);
    }


    /**
     * 修改公司基本信息
     */
    public function setusers_baseinfo_gs(Request $request, $post, $returnrray)
    {
        $usercom = UserGongsiModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if (!$usercom) {
            $usercom = new UserGongsiModel;
            $usercom->created_at = time();
        }
        $usercom->shenhe = CompanyModel::shenhe_no;
        $usercom->visible = 0;
        $usercom->com_name = $post['com_name'];
        $usercom->legal_name = $post['legal_name'];
        $usercom->legal_sex = $post['legal_sex'];
        $usercom->legal_mobile = $post['legal_mobile'];
        $usercom->com_tel = $post['com_tel'];
        $usercom->comCity = $post['comCity'];
        $usercom->com_address = $post['com_address'];
        $usercom->position = $post['position'];
        $usercom->detail = $post['detail'];
        $usercom->nianxiaoshou = $post['nianxiaoshou1'] . ',' . $post['nianxiaoshou2'];
        if ($usercom->save() !== false) {
            /* $one = Users_baseinfo_gsModel::where([
                 'id'=>OA::$user->getId()
             ])->update(['isbase'=>1]);*/
            Users_baseinfo_gsModel::setIS('isbase', OA::$user->getId());
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '保存错误');
        }
    }


    /**
     * 获取公司证件上传的情况
     */
    public function getusers_zhengjianshangchuan_gs(Request $request, $post, $returnrray)
    {
        $findmodel = Users_zhengjianshangchuan_gsModel::where([
            'uid' => OA::$user->getId()
        ])->select([
            'xingxiangzhao',
            'shenfenzhengzhengmian',
            'shenfenzhengfanmian',
            'jiangzhuang',
            'jiehunzheng',

            'jiashizheng',
            'zuzhijiegoudaimazhao',
            'yingyezhizhao',
            'shuiwudengjizheng',
            'zulinhetongzhao',

            'geleixukezheng',
            'geleirongyu',
            'fangchanzheng',
        ])->first();
        if ($findmodel) {
            $returnrray['xingxiangzhao'] = $findmodel['xingxiangzhao'];
            $returnrray['shenfenzhengzhengmian'] = $findmodel['shenfenzhengzhengmian'];
            $returnrray['shenfenzhengfanmian'] = $findmodel['shenfenzhengfanmian'];
            $returnrray['jiangzhuang'] = $findmodel['jiangzhuang'];
            $returnrray['jiehunzheng'] = $findmodel['jiehunzheng'];

            $returnrray['jiashizheng'] = $findmodel['jiashizheng'];
            $returnrray['zuzhijiegoudaimazhao'] = $findmodel['zuzhijiegoudaimazhao'];
            $returnrray['yingyezhizhao'] = $findmodel['yingyezhizhao'];
            $returnrray['shuiwudengjizheng'] = $findmodel['shuiwudengjizheng'];
            $returnrray['zulinhetongzhao'] = $findmodel['zulinhetongzhao'];

            $returnrray['geleixukezheng'] = $findmodel['geleixukezheng'];
            $returnrray['geleirongyu'] = $findmodel['geleirongyu'];
            $returnrray['fangchanzheng'] = $findmodel['fangchanzheng'];
        }
        /// print_r($returnrray);exit;
        //  $returnrray['shenfenzhengfanmian']='http://www.fuzhuangchangongxiao.cn/uploads/20191103/a16e46eab7aba28d7b53a5deb75cd67asmall.jpg';
        OA::Json(0, $returnrray);
    }

    /**
     * 修改公司证件上传的情况
     */
    public function setusers_zhengjianshangchuan_gs(Request $request, $post, $returnrray)
    {
        $field = $post['field'];
        $src = $post['src'];
        $findmodel = Users_zhengjianshangchuan_gsModel::where([
            'uid' => OA::$user->getId()
        ])->select([
            'id', $field
        ])->first();
        if (!$findmodel) {
            $findmodel = new Users_zhengjianshangchuan_gsModel;
            $findmodel->created_at = time();
            $findmodel->uid = OA::$user->getId();
        }
        $findmodel[$field] = $src;
        if ($findmodel->save() === false) {
            OA::Json(1, '保存错误');
        } else {
            Users_baseinfo_gsModel::setIS('iszhengjian', OA::$user->getId());
            OA::Json(0, 'ok');
        }

    }

    /**
     * 获取公司产品
     */
    public function getusers_basechanpin_gs(Request $request, $post, $returnrray)
    {
        $chanpinStr = '';
        $zhenzhileiStr = '';
        $suozhileiStr = '';
        $zhenzhileiList = '针织帽,围巾,手套,披巾,内衣,内裤,套装,毛衣,外套,打底裤,针织衫,裙子,运动服,运动裤,睡衣,睡裤,短裤,袜子,窗帘,床上用品,桌布,桌布';

        $suozhileiList = '羽绒服,羽绒裤,双面尼,单面尼,羊毛大衣,夹克,外套';

        $one = User_basechanpin_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if ($one) {
            $chanpinStr = $one->chanpin;
            $zhenzhileiStr = $one->zhenzhilei;
            $suozhileiStr = $one->suozhilei;
        }
        $returnrray['chanpin'] = $chanpinStr;
        $returnrray['zhenzhileidefault'] = $zhenzhileiList;
        $returnrray['suozhileidefault'] = $suozhileiList;
        $returnrray['zhenzhilei'] = $zhenzhileiStr;
        $returnrray['suozhilei'] = $suozhileiStr;
        OA::Json(0, $returnrray);
    }

    /**
     * 设置公司产品
     */
    public function setusers_basechanpin_gs(Request $request, $post, $returnrray)
    {
        $one = User_basechanpin_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if (!$one) {
            $one = new User_basechanpin_gsModel;
            $one->uid = OA::$user->getId();
            $one->created_at = time();
            $one->fengge = '';
        }
        $one->chanpin = $post['chanpin'];
        $one->zhenzhilei = $post['zhenzhilei'];
        $one->suozhilei = $post['suozhilei'];
        if ($one->save() !== false) {
            Users_baseinfo_gsModel::setIS('isgongsichanpin', OA::$user->getId());
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '保存错误');
        }
    }

    /**
     *获取产品风格
     */
    public function getusers_basechanpinfengge_gs(Request $request, $post, $returnrray)
    {
        $fengge = '';
        $one = User_basechanpin_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if ($one) {
            $fengge = $one->fengge;
        }
        $list = explode(",", $fengge);
        $fenggelist = [
            [
                'text' => '瑞丽'
            ],
            [
                'text' => '甜蜜'
            ],

            [
                'text' => '淑女'
            ],
            [
                'text' => '名媛'
            ],

            [
                'text' => '清晰'
            ],
            [
                'text' => '田园'
            ],

            [
                'text' => '休闲'
            ],

            [
                'text' => '运动'
            ],

            [
                'text' => '中性'
            ],
            [
                'text' => '打底裤'
            ],

            [
                'text' => '打底裤'
            ],
            [
                'text' => '欧美'
            ],

            [
                'text' => '街头'
            ],
            [
                'text' => '朋克'
            ],
            [
                'text' => '学院'
            ],

            [
                'text' => '英伦'
            ],
            [
                'text' => '民族'
            ],
            [
                'text' => '复古'
            ],
            [
                'text' => '中国风'
            ]
        ];
        foreach ($fenggelist as $k => $v) {
            if (in_array($v['text'], $list)) {
                $fenggelist[$k]['on'] = true;
            } else {
                $fenggelist[$k]['on'] = false;
            }
        }
        $returnrray['fenggelist'] = $fenggelist;
        OA::Json(0, $returnrray);
    }

    /**
     * 设置公司产品风格
     */
    public function setusers_basechanpinfengge_gs(Request $request, $post, $returnrray)
    {
        $one = User_basechanpin_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if (!$one) {
            $one = new User_basechanpin_gsModel;
            $one->uid = OA::$user->getId();
            $one->created_at = time();
            $one->fengge = '';
            $one->chanpin = '';
            $one->zhenzhilei = '';
            $one->suozhilei = '';
        }
        $one->fengge = $post['fengge'];
        if ($one->save() !== false) {
            Users_baseinfo_gsModel::setIS('ischanpinfengge', OA::$user->getId());
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '保存错误');
        }
    }


    /**
     *获取产品风格
     */
    public function getusers_basezhiliangbiaozhun_gs(Request $request, $post, $returnrray)
    {
        $zhiliangbiaozhun='';
        $one = User_basechanpin_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if ($one) {
            $zhiliangbiaozhun = $one->zhiliangbiaozhun;
        }
        $list = explode(",", $zhiliangbiaozhun);
        $zhiliangbiaozhunlist = [
            [
                'text' => '按工艺单'
            ],
            [
                'text' => '平量'
            ],
            [
                'text' => '全检'
            ],
            [
                'text' => '抽检'
            ]
        ];
        foreach ($zhiliangbiaozhunlist as $k => $v) {
            if (in_array($v['text'], $list)) {
                $zhiliangbiaozhunlist[$k]['on'] = true;
            } else {
                $zhiliangbiaozhunlist[$k]['on'] = false;
            }
        }
        $returnrray['zhiliangbiaozhun'] = $zhiliangbiaozhunlist;
        OA::Json(0, $returnrray);
    }


    /**
     * 设置公司产品风格
     */
    public function setusers_basezhiliangbiaozhun_gs(Request $request, $post, $returnrray)
    {
        $one = User_basechanpin_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if (!$one) {
            $one = new User_basechanpin_gsModel;
            $one->uid = OA::$user->getId();
            $one->created_at = time();
            $one->fengge = '';
            $one->chanpin = '';
            $one->zhenzhilei = '';
            $one->suozhilei = '';
            $one->zhiliangbiaozhun = '';
        }
        $one->zhiliangbiaozhun = $post['zhiliangbiaozhun'];
        if ($one->save() !== false) {
            Users_baseinfo_gsModel::setIS('iszhiliangbiaozhun', OA::$user->getId());
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '保存错误');
        }
    }


    private function explode($de, $field)
    {
        if (empty($field)) {
            return [];
        }
        return explode($de, $field);
    }

    /**
     *获取公司环境照片
     */
    public function getusers_gongsihuanjing_gs(Request $request, $post, $returnrray)
    {
        $one = Users_gongsihuanjing_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if ($one) {
            $returnrray['zongjiangban'] = $this->explode(",", $one->zongjiangban);
            $returnrray['xiaoshoubu'] = $this->explode(",", $one->xiaoshoubu);
            $returnrray['zhaoshangbu'] = $this->explode(",", $one->zhaoshangbu);

            $returnrray['dianshangbu'] = $this->explode(",", $one->dianshangbu);
            $returnrray['shejibu'] = $this->explode(",", $one->shejibu);
            $returnrray['jiishubu'] = $this->explode(",", $one->jiishubu);

            $returnrray['caigoubu'] = $this->explode(",", $one->caigoubu);
            $returnrray['shengchanbu'] = $this->explode(",", $one->shengchanbu);
            $returnrray['waifabu'] = $this->explode(",", $one->waifabu);

            $returnrray['caiwubu'] = $this->explode(",", $one->caiwubu);
            $returnrray['xingzhengbu'] = $this->explode(",", $one->xingzhengbu);
            $returnrray['renshibu'] = $this->explode(",", $one->renshibu);

            $returnrray['pinkongbu'] = $this->explode(",", $one->pinkongbu);
            $returnrray['wuliubu'] = $this->explode(",", $one->wuliubu);
            $returnrray['cangchubu'] = $this->explode(",", $one->cangchubu);

            $returnrray['huiyishi'] = $this->explode(",", $one->huiyishi);
            $returnrray['zhanshiting'] = $this->explode(",", $one->zhanshiting);

        } else {
            $returnrray['zongjiangban'] = [];
            $returnrray['xiaoshoubu'] = [];
            $returnrray['zhaoshangbu'] = [];

            $returnrray['dianshangbu'] = [];
            $returnrray['shejibu'] = [];
            $returnrray['jiishubu'] = [];

            $returnrray['caigoubu'] = [];
            $returnrray['shengchanbu'] = [];
            $returnrray['waifabu'] = [];

            $returnrray['caiwubu'] = [];
            $returnrray['xingzhengbu'] = [];
            $returnrray['renshibu'] = [];

            $returnrray['pinkongbu'] = [];
            $returnrray['wuliubu'] = [];
            $returnrray['cangchubu'] = [];

            $returnrray['huiyishi'] = [];
            $returnrray['zhanshiting'] = [];
        }

        OA::Json(0, $returnrray);
    }


    /**
     *获取公司环境照片
     */
    public function setusers_gongsihuanjing_gs(Request $request, $post, $returnrray)
    {
        $one = Users_gongsihuanjing_gsModel::where([
            'uid' => OA::$user->getId()
        ])->first();
        if (!$one) {
            $one = new Users_gongsihuanjing_gsModel;
            $one->uid = OA::$user->getId();
            $one->time = time();
            $one->zongjiangban = '';
            $one->xiaoshoubu = '';
            $one->zhaoshangbu = '';

            $one->dianshangbu = '';
            $one->shejibu = '';
            $one->jiishubu = '';

            $one->caigoubu = '';
            $one->shengchanbu = '';
            $one->waifabu = '';

            $one->caiwubu = '';
            $one->xingzhengbu = '';
            $one->renshibu = '';

            $one->pinkongbu = '';
            $one->wuliubu = '';
            $one->cangchubu = '';

            $one->huiyishi = '';
            $one->zhanshiting = '';
        }else{
            $one->zongjiangban=$post['zongjiangban'];
            $one->xiaoshoubu=$post['xiaoshoubu'];
            $one->zhaoshangbu=$post['zhaoshangbu'];

            $one->dianshangbu=$post['dianshangbu'];
            $one->shejibu=$post['shejibu'];
            $one->jiishubu=$post['jiishubu'];

            $one->caigoubu=$post['caigoubu'];
            $one->shengchanbu=$post['shengchanbu'];
            $one->waifabu=$post['waifabu'];

            $one->caiwubu=$post['caiwubu'];
            $one->xingzhengbu=$post['xingzhengbu'];
            $one->renshibu=$post['renshibu'];

            $one->pinkongbu=$post['pinkongbu'];
            $one->wuliubu=$post['wuliubu'];
            $one->cangchubu=$post['cangchubu'];

            $one->huiyishi=$post['huiyishi'];
            $one->zhanshiting=$post['zhanshiting'];
        }
        if ($one->save() !== false) {
            Users_baseinfo_gsModel::setIS('isgongsihuanjing', OA::$user->getId());
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '保存错误');
        }
    }


}
